﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ReporterEngine.Methods
{
    public class AllParents : BaseMethod
    {
        public override bool IsUseable(LexPath.PathToken token)
        {
            return token.Kind == LexPath.TokenKind.Method && !string.IsNullOrEmpty(token.PathExp) && token.PathExp.ToLower() == "allparents";
        }

        public override sQuery Execute(LexPath.PathToken token, sQuery query)
        {
            var result = query.New();
            //

            foreach (var element in query.Elements)
            {
                var parent = query._model.GetXParent(element);

                while (parent != null && !result.Elements.Contains(parent))
                {

                    //var name = query._model.GetName(parent);
                    //    //archiving of operative documentation
                    //if (name.ToLower() == "archiving of operative documentation")
                    //{
                    //    int ee = 12;
                    //}

                    result.Elements.Add(parent);

                    parent = query._model.GetXParent(parent);
                }
            }

            return result;
        }

        public override MethodDescription[] GetMethodNames()
        {
            return new MethodDescription[]{new MethodDescription{Name = "allParents"} };
        }
    }
}
